# import cv2
# import numpy as np
# from matplotlib import pyplot as plt
# region = np.array([[1, 1], [1, 4], [4, 4], [4, 1]])  # 多边形顶点坐标
# data = np.zeros((50, 100))

# # 将多边形内部区域填充为1
# cv2.fillPoly(data, [region], 1)
# plt.imshow(data, cmap='gray')
# plt.show()


# import numpy as np

# data = np.array([[1, 2, 3],
#                  [4, 6, 6],
#                  [7, 8, 9]])

# mask = np.array([[0, 1, 0],
#                  [1, 0, 1],
#                  [0, 1, 0]])

# masked_data = np.ma.masked_array(data, mask=mask)

# sum_masked_values = np.sum(masked_data)

# print("标记值的总和：", sum_masked_values)



# import numpy as np
# import matplotlib.pyplot as plt
# region = np.array([[1, 1], [1, 4], [4, 4], [4, 1],[2, 5]])  # 示例区域的散点坐标

# center = np.mean(region, axis=0)
# print(center)
# # 绘制区域
# plt.scatter(region[:, 0], region[:, 1], c='blue', label='Region')
# # 绘制中心点
# plt.scatter(center[0], center[1], c='red', label='Center')
# plt.gca().invert_yaxis()
# # 添加图例和标签
# plt.legend()
# plt.xlabel('X')
# plt.ylabel('Y')
# plt.title('Region and Center')

# # 显示图形
# plt.show()

# import re

# # 打开原始文本文件并读取内容
# with open("labels.txt", "r+") as file:
#     text = file.read()

#     # 使用正则表达式匹配方括号中的值
#     pattern = r"\[([\d. ]+)\]"
#     matches = re.findall(pattern, text)

#     # 处理匹配到的值
#     for match in matches:
#         processed_value = re.sub(r"\s+", "#", match)
#         text = text.replace(f"[{match}]", f"[{processed_value}]")

#     # 将处理后的文本写回原始文件
#     file.seek(0)
#     file.write(text)
#     file.truncate()

# # 打开原始文本文件并读取内容
# with open("labels.txt", "r") as file:
#     lines = file.readlines()

# # 去除每一行的末尾逗号
# processed_lines = [line.rstrip(",\n") for line in lines]
# # 将处理后的文本写回原始文件
# with open("labels.txt", "w") as file:
#     file.write("\n".join(processed_lines))

import numpy as np
import cv2

height = 544
width = 1089
data = np.random.randint(0, 256, (height, width), dtype=np.uint8)
x = [59, 99, 208, 502, 743, 849, 945, 981]
y = [0, 210, 359, 521, 471, 369, 250, 0]

# 创建空白的掩膜
mask = np.zeros((544, 1089), dtype=np.uint8)

# 创建多边形的顶点坐标数组
pts = np.column_stack((x, y))

# 在掩膜上绘制多边形
cv2.fillPoly(mask, [pts], 255)

# 将掩膜区域外的像素设置为0
data[np.where(mask == 0)] = 0

# 显示图像
cv2.imshow("Image", data)
cv2.waitKey(0)
cv2.destroyAllWindows()